package logic

import (
	"commerci/moddel/mysql"
	"context"

	"commerci/commercirpc/commercirpc"
	"commerci/commercirpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodsListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodsListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodsListLogic {
	return &GoodsListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GoodsListLogic) GoodsList(in *commercirpc.GoodsListRequest) (*commercirpc.GoodsListResponse, error) {
	// todo: add your logic here and delete this line

	var good []mysql.Goods
	mysql.DB.Debug().Find(&good)

	var goodList []*commercirpc.GoodList

	for _, goods := range good {
		list := commercirpc.GoodList{
			GoodId: int64(goods.ID),
			Name:   goods.Name,
			Price:  goods.Price,
			State:  goods.State,
			UserId: goods.UserId,
		}
		goodList = append(goodList, &list)
	}

	return &commercirpc.GoodsListResponse{
		List: goodList,
	}, nil
}
